package com.example.comic.mvp.view

import android.content.Intent
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.MenuItem
import android.view.View
import com.example.comic.R
import com.example.comic.adpter.ChapterAdapter
import com.example.comic.mvp.model.Chapter
import com.example.comic.mvp.ppresenter.ChapterPresenter
import com.example.comic.utils.LogUtil
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_chapter.*
import org.jetbrains.anko.async
import org.jetbrains.anko.find
import org.jetbrains.anko.uiThread

class ChapterActivity : AppCompatActivity() {
    companion object {
        val INTENT_LINK = "link"
        val INTENT_COVER = "cover"
        val INTENT_TITLE = "title"
        val INTENT_CATEGORY = "category"
    }
    lateinit var link: String
    lateinit var cover: String
    lateinit var title: String
    lateinit var category: String

    lateinit var adapter: ChapterAdapter
    lateinit var recyclerView: RecyclerView
    lateinit var swipeRefreshLayout: SwipeRefreshLayout
    var mData: List<Chapter> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chapter)
        init()
    }

    private fun init() {
        link = intent.getStringExtra(INTENT_LINK)
        cover = intent.getStringExtra(INTENT_COVER)
        title = intent.getStringExtra(INTENT_TITLE)
        category = intent.getStringExtra(INTENT_CATEGORY)
        LogUtil.e("link", link)
        setSupportActionBar(toolbar)

        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setDisplayShowHomeEnabled(true)
        collapsingToolbar.title = title
        if (!cover.isEmpty())
            Picasso.get().load(cover).into(imageView)
        swipeRefreshLayout = find(R.id.swipeRefresh)
        recyclerView = find(R.id.recyclerView)
        swipeRefreshLayout.setOnRefreshListener { load() }

        recyclerView.layoutManager = GridLayoutManager(this, 2)
        adapter = ChapterAdapter { _: View, i: Int -> jumpToPager(i) }
        recyclerView.adapter = adapter
    }

    override fun onResume() {
        super.onResume()
        swipeRefreshLayout.isRefreshing = true
        load()
    }

    private fun jumpToPager(position: Int) {
        val intent = Intent(this, PagerActivity::class.java)
        intent.putExtra(PagerActivity.INTENT_LINK, mData[position].link)
        startActivity(intent)
    }

    private fun load() {
        async {
            val data = ChapterPresenter().obtain(link)
            uiThread {
                mData = data
                adapter.refreshData(mData)
                swipeRefreshLayout.isRefreshing = false
            }

        }
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        return when (item?.itemId) {
            android.R.id.home -> {
                onBackPressed()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    override fun onBackPressed() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            finishAfterTransition()
        } else {
            super.onBackPressed()
        }
    }
}
